package com.toolset.Activity.ToolItemActivity;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.toolset.Activity.BaseActivity;
import com.toolset.R;

import java.util.Random;

public class GuessGame extends BaseActivity {


    private EditText smallEdit;
    private EditText bigEdit;
    private EditText answerEdit;

    private Button resetButton;
    private Button answerButton;
    private Button guessButton;
    //答案
    int resultNum=0;
    //次数
    int count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guess_game);
        //初始化
        initEvent();

        //重置答案
        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String smallText = smallEdit.getText().toString();
                String bigText = bigEdit.getText().toString();

                //判断输入值
                if (smallText.length() == 0 || bigText.length() == 0) {
                    showAlertDialog("请输入范围值");
                }

                else {
                    int smallNum = Integer.parseInt(smallText);
                    int bigNum = Integer.parseInt(bigText);

                    if (smallNum > bigNum) {
                        showAlertDialog("范围1不能小于范围2");
                    }else {

                        //生成随机数
                        Random random = new Random();
                        resultNum = random.nextInt(bigNum + 1);

                        //小于最小数 重新生成
                        while (resultNum < smallNum) {
                            resultNum = random.nextInt(bigNum + 1);
                        }

                        showAlertDialog("重置答案成功");
                        //重置次数
                        count = 0;

                    }
                }
            }
        });


        //看答案
        answerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showAlertDialog("本次猜数小游戏答案："+resultNum);
            }
        });


        //猜数字
        guessButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

               String answerText =  answerEdit.getText().toString();
                if (answerText.length()==0){
                    showAlertDialog("请先输入数字");
                }else{

                    count ++ ;

                    int answerNum = Integer.parseInt(answerText);

                    if (answerNum<resultNum){
                        showAlertDialog("猜小了");
                    }
                    else if (answerNum>resultNum){
                        showAlertDialog("猜大了");
                    }else{

                        showAlertDialog("恭喜你猜对了！\n 猜了" + count + "次");
                    }
                }

            }
        });


    }

    private  void initEvent(){
        smallEdit=(EditText)findViewById(R.id.guess_small_edit);
        bigEdit=(EditText) findViewById(R.id.guess_big_edit);
        answerEdit=(EditText) findViewById(R.id.answer_edit);
        resetButton=(Button) findViewById(R.id.reset_button);
        answerButton=(Button) findViewById(R.id.answer_button);
        guessButton=(Button) findViewById(R.id.guess_once);
    }
    //提示框函数
    private void showAlertDialog(String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(GuessGame.this);
        builder.setTitle("提示");
        builder.setMessage(message);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
}
